package controller;

import command.FormCommand;
import entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

@Controller
@RequestMapping("/form")
public class FormController {

    @RequestMapping("/show")
    public String show(FormCommand formCommand){
        System.out.println("FormController.show()");

        formCommand.setT("abc");
        formCommand.setP("123");
        formCommand.setTa("abcd");
        formCommand.setH("1");
        formCommand.setC(Arrays.asList("2","3"));
        formCommand.setCs(new String[]{"3","6"});
        formCommand.setR("2");
        formCommand.setS("2");

        return "form";
    }

    @RequestMapping("/submit")
    public String submit(FormCommand formCommand){
        System.out.println("FormController.submit()");

        System.out.println("formCommand = " + formCommand);
        
        return "redirect:/form/show";
    }

    @ModelAttribute("map")
    public Map<String,Object> loadMap(){
        System.out.println("FormController.loadMap()");
        Map<String,Object> map=new HashMap<String,Object>();

        map.put("1","aaa");
        map.put("2","bbb");
        map.put("3","ccc");
        map.put("4","ddd");
        map.put("5","eee");
        map.put("6","fff");

        return map;
    }


    @ModelAttribute("users")
    public List<User> loadUsers(){
        List<User> users=new ArrayList<User>();

        for (int i = 0; i < 10; i++) {
            User user=new User(i+1,"user"+(i+1),i+"","123456789",2*i);
            users.add(user);
        }

        return users;
    }


}
